package us.lithub.ui.activity;

import java.util.List;

import us.lithub.R;
import us.lithub.data.Book;
import us.lithub.data.Friend;
import us.lithub.data.WebConnector;
import us.lithub.ui.RowBook;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.markupartist.android.widget.ActionBar;

/**
 * The Class TheirLibrary is similar to my library but it shows books that a particular user owns
 */
public class TheirLibrary extends Activity {

	private static final String TAG = TheirLibrary.class.getSimpleName();
	private WebConnector wc;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "starting TheirLibrary activity");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_their_library);
		
		Intent intent = getIntent();
		String username = intent.getStringExtra(Friend.NAME);
		String title = username + "'s library";
		
		ActionBar actionbar = (ActionBar) findViewById(R.id.actionbar);
		actionbar.setTitle(title);
		
		wc = WebConnector.getInstance();
		loadData(username);
	}
	
	private void loadData(final String username) {
		new Thread() {
			
			@Override
			public void run() {
				final List<Book> books = wc.getBooksForUser(username);
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// Create the adapter and bind data to the list view
						BookAdapter adapter = new BookAdapter(TheirLibrary.this, R.layout.row_book, books);
						ListView lv = (ListView) findViewById(R.id.lvListView);
						lv.setAdapter(adapter);
						lv.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								if (books.get(position).getStatus() != 101) {
									Toast.makeText(TheirLibrary.this, "They don't currently have this book, cannot request", Toast.LENGTH_SHORT).show();
									return;
								}
								confirmRequest();
							}
						});	
					}
				});
			};
		}.start();
	}

	private void confirmRequest() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to request this book to <INSERT NAME HERE> ?");
		builder.setCancelable(false);
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				new Thread() {
					@Override
					public void run() {
						wc.lendBook(null, null, null, null);
						Toast.makeText(TheirLibrary.this, "Book lent to friend", Toast.LENGTH_SHORT).show();
					}
				}.start();
				dialog.cancel();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	private class BookAdapter extends ArrayAdapter<Book> {

		private Context context;
		private List<Book> books;

		public BookAdapter(Context context, int textViewResourceId, List<Book> books) {
			super(context, textViewResourceId, books);
			this.context = context;
			this.books = books;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;

			if (v == null) {
				LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row_book, null);
			}

			Book book = books.get(position);

			if (book != null) {
				((RowBook) v).setTitle(book.getTitle());
				((RowBook) v).setAuthor(book.getAuthor());
				((RowBook) v).setImage(book.getIsbn());
			}
			return v;
		}	
	}
}
